package com.jrx.anytxn.param.service.account.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.account.SubjectTableReq;
import com.jrx.anytxn.param.dto.account.SubjectTableRes;
import com.jrx.anytxn.param.entity.GaSubjectTable;
import com.jrx.anytxn.param.service.account.ISubjectTableService;
import com.jrx.anytxn.param.mapper.ext.ExtSubjectTableMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by yf
 * @Classname SubjectTableServiceImpl
 * @Description 科目表接口实现层
 * @Date 2019/7/5 0005 下午 14:18
 */
@Service
public class SubjectTableServiceImpl implements ISubjectTableService {

    private static final Logger logger = LoggerFactory.getLogger(SubjectTableServiceImpl.class);

    @Resource
    private ExtSubjectTableMapper extSubjectTableMapper;

    @Autowired
    private ISegmentService segmentService;



    @Override
    public Boolean removeByTenIdAndSuubjectId(String tenantId, String subjectId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(subjectId)) {
            logger.error("科目表主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extSubjectTableMapper.deleteByTenIdAndSubjectId(tenantId, subjectId);
        return a > 0;
    }

    @Override
    public SubjectTableRes modifySubjectTable(SubjectTableReq subjectTableReq) {
        GaSubjectTable subjectTable = BeanMapping.copy(subjectTableReq, GaSubjectTable.class);
        extSubjectTableMapper.updateByTenIdAndSubjectId(subjectTable);
        return BeanMapping.copy(subjectTable, SubjectTableRes.class);
    }

    @Override
    public SubjectTableRes addSubjectTable(SubjectTableReq subjectTableReq) throws TxnException {


        try {
            subjectTableReq.setId(segmentService.getId(Constant.GA_SUBJECT_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        GaSubjectTable subjectTable = BeanMapping.copy(subjectTableReq, GaSubjectTable.class);
        /*根据索引查看是否存在此信息*/
        GaSubjectTable table = extSubjectTableMapper.selectBySubjectIdAndTenId(subjectTable.getTenantId(), subjectTable.getSubjectId());
        if (table != null) {
            logger.error("增加的科目表信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        subjectTable.setRecordVersionNumber(Constant.VERSION_NUMBER);

        extSubjectTableMapper.insertSelective(subjectTable);
        return BeanMapping.copy(subjectTable, SubjectTableRes.class);
    }

    @Override
    public TxnPage<SubjectTableRes> getAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId,String subjectId) throws TxnException {
        Page<SubjectTableRes> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<GaSubjectTable> subjectTableList = extSubjectTableMapper.selectByTenId(tenantId,subjectId);
        if (subjectTableList.isEmpty()) {
            logger.error("查询的科目表列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<SubjectTableRes> res = BeanMapping.copyList(subjectTableList, SubjectTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public SubjectTableRes getByTenIdIdAndSubjectId(String subjectId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(subjectId) || StringUtils.isBlank(tenantId)) {
            logger.error("科目ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaSubjectTable subject = extSubjectTableMapper.selectBySubjectIdAndTenId(tenantId, subjectId);
        return BeanMapping.copy(subject, SubjectTableRes.class);
    }

    @Override
    public List<GaSubjectTable> getAllSubjectId(String status) {
        return extSubjectTableMapper.selectAllSubjectId(status);
    }

    @Override
    public List<SubjectTableRes> getAllSubject(String tenantId,String subjectId) {
        List<GaSubjectTable> subjectTables = extSubjectTableMapper.selectByTenId(tenantId,subjectId);
        return BeanMapping.copyList(subjectTables, SubjectTableRes.class);
    }

    @Override
    public List<SubjectTableRes> getEffectiveSubjectIds(String tenantId) {
        List<GaSubjectTable> subjectTables = extSubjectTableMapper.selectEffectiveByTenId(tenantId);
        return BeanMapping.copyList(subjectTables, SubjectTableRes.class);
    }

    @Override
    public List<SubjectTableRes> findSubjectList(String balDirection, String tenantId,String orgId) {
        List<GaSubjectTable> subjectTableList = extSubjectTableMapper.selectByBalDirection(balDirection, tenantId,orgId);
        return BeanMapping.copyList(subjectTableList, SubjectTableRes.class);
    }

    @Override
    public List<SubjectTableRes> getEffectiveSubjectIdsByOrgId(String tenantId, String orgId) {
        List<GaSubjectTable> subjectTables = extSubjectTableMapper.selectEffectiveByTenIdAndOrgId(tenantId,orgId);
        return BeanMapping.copyList(subjectTables, SubjectTableRes.class);
    }
}
